/*
 * @Author: “lixiaojuan6851” 1401125373@qq.com
 * @Date: 2024-12-18 21:39:49
 * @LastEditors: “lixiaojuan6851” 1401125373@qq.com
 * @LastEditTime: 2024-12-18 22:52:52
 * @FilePath: \qsm\node.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const app = express();
app.use(bodyParser.json());



const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'login_exam',
  port: 3306
});
//打开登录界面
// app.get('/login',(req,res)=>{
//   res.render('login.html')
// })
// 设置静态资源目录
app.use(express.static('public'));
app.post('/login', (req, res) => {
  const { username, password } = req.body;
  db.query('SELECT * FROM users WHERE username = ? AND password = ?', [username,password], (err, results) => {
    if (err) {
      return res.status(500).json({ success: false, message: '服务器错误' });
    }
    if (results.length > 0) {
      res.json({ success: true });
    } else {
      res.json({ success: false, message: '用户名或者密码不正确' });
    }
  });
});
app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});